package com.xhwl.centiotdata.pollingtrigger;

import com.xhwl.centiotdata.service.integrateconfig.IntegrationConfigHandler;
import com.xhwl.centiotdata.utils.ThreadPoolUtils;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ExecutorService;

/**
 * @Author： lianghz
 * @Description：
 * @Date 2021/09/03/ 09:21
 */
@Component
@Slf4j
public class ScheduleHandler {

    @Autowired
    private List<ScheduleDispatcher> scheduleDispatcherList;
    @Autowired
    private IntegrationConfigHandler integrationConfig;

    @XxlJob("iot-data-five-seconds")
    public ReturnT<String> fiveSecondsTask(String param) throws Exception {
        try {
            scheduleDispatcherList.forEach(scheduleDispatcher -> getTaskPool().execute(() ->
                    scheduleDispatcher.doFiveSecondsTask()));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("iot-data-ten-seconds")
    public ReturnT<String> tenSecondsTask(String param) throws Exception {
        try {
            scheduleDispatcherList.forEach(scheduleDispatcher -> getTaskPool().execute(() ->
                    scheduleDispatcher.doTenSecondsTask()));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("iot-data-one-minutes")
    public ReturnT<String> oneMinutesTask(String param) throws Exception {
        try {
            scheduleDispatcherList.forEach(scheduleDispatcher -> getTaskPool().execute(() ->
                    scheduleDispatcher.doOneMinutesTask()));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("iot-data-five-minutes")
    public ReturnT<String> fiveMinutesTask(String param) throws Exception {
        try {
            // 每五分钟的定时任务
            scheduleDispatcherList.forEach(scheduleDispatcher -> getTaskPool().execute(() ->
                    scheduleDispatcher.doFiveMinutesTask()));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("iot-data-ten-minutes")
    public ReturnT<String> tenMinutesTask(String param) throws Exception {
        try {
            // 更新配置信息
            integrationConfig.executeUpdate();

            scheduleDispatcherList.forEach(scheduleDispatcher -> getTaskPool().execute(() ->
                    scheduleDispatcher.doTenMinutesTask()));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("iot-data-oneHalf-hour")
    public ReturnT<String> oneHalfHourTask(String param) throws Exception {
        try {
            scheduleDispatcherList.forEach(scheduleDispatcher -> getTaskPool().execute(() ->
                    scheduleDispatcher.doOneHalfHourTask()));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("iot-data-one-hour")
    public ReturnT<String> oneHourTask(String param) throws Exception {
        try {
            scheduleDispatcherList.forEach(scheduleDispatcher -> getTaskPool().execute(() ->
                    scheduleDispatcher.doOneHourTask()));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("iot-data-one-day")
    public ReturnT<String> oneDayTask(String param) throws Exception {
        try {
            scheduleDispatcherList.forEach(scheduleDispatcher -> getTaskPool().execute(() ->
                    scheduleDispatcher.doOneDayTask()));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    private ExecutorService getTaskPool() {
        return ThreadPoolUtils.taskDispatcher;
    }
}
